﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace BrainTechLLC.Silverlight.ClientApplication.Controls
{
    public partial class LoginControl : UserControl
    {
        public LoginControl()
        {
            // Required to initialize variables
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            DataAccess.LoginSuccessful += new EventHandler(DataAccess_LoginSuccessful);
            DataAccess.LoginUnsuccessful += new EventHandler(DataAccess_LoginUnsuccessful);
            DataAccess.UsersGameCharactersRead += new EventHandler(DataAccess_UsersGameCharactersRead);
            DataAccess.GameCharactersRead += new EventHandler(DataAccess_GameCharactersRead);
            loginNameInput.Value = "owen";
            passwordInput.Value = "owen";
            Dispatcher.BeginInvoke(() => loginNameInput.Focus());
        }

        void DataAccess_GameCharactersRead(object sender, EventArgs e)
        {
            // Nothing to do here
        }

        void DataAccess_UsersGameCharactersRead(object sender, EventArgs e)
        {
            this.Visibility = Visibility.Collapsed;
            App._rootPage.manageCharacters.Visibility = Visibility.Visible;
            App._rootPage.manageCharacters.WantEnterGame += new EventHandler<ObjectEventArgs<GameChar>>(manageCharacters_WantEnterGame);
            App._rootPage.manageCharacters.BindToGameCharData(DataAccess.UsersGameCharacters);
        }

        void manageCharacters_WantEnterGame(object sender, ObjectEventArgs<GameChar> e)
        {
            DataAccess.RaiseEnteredGame(sender, e.obj);            
        }

        void DataAccess_LoginUnsuccessful(object sender, EventArgs e)
        {
            App.MainDispatcher.BeginInvoke(() => { MessageBox.Show("Login unsuccessful"); btnLogin.IsEnabled = true; });
        }

        void DataAccess_LoginSuccessful(object sender, EventArgs e)
        {
            Dispatcher.BeginInvoke(() => btnLogin.IsEnabled = true);
            DataAccess.Client.ReadGameCharactersAsync(DataAccess.AssignedUniqueId, DataAccess.CurrentLogin);            
        }

        public void DoLogin()
        {
            string username = loginNameInput.Value;
            string password = passwordInput.ReadPassword();

            DataAccess.Login(username, password);
        }

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            btnLogin.IsEnabled = false;
            DoLogin();
        }

        private void btnCreateNew_Click(object sender, RoutedEventArgs e)
        {
            btnCreateNew.IsEnabled = false;
            // HEREHERE
        }

        private void txtLoginName_KeyDown(object sender, KeyEventArgs e)
        {            
            if (e.Key == Key.Enter)
            {                
                DoLogin();
                e.Handled = true;
            }
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                DoLogin();
                e.Handled = true;
            }
        }

        private void LayoutRoot2_Loaded(object sender, RoutedEventArgs e)
        {
            loginNameInput.Focus();
        }

        private void UserControl_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                DoLogin();
                e.Handled = true;
            }
        }
    }
}